function HandleLowMemory( event ) 
  print( "memory warning received!" ) ;
end 

system.activate( "multitouch" );

local physics = require "physics"
physics.start();
physics.setGravity( 0, 5.4 );
physics.setPositionIterations(8)
--physics.setVelocityIterations( 12 )
--physics.setDrawMode( "debug" ) -- shows collision engine outlines only
--physics.setDrawMode( "hybrid" ) -- overlays collision outlines on normal Corona objects
physics.setDrawMode( "normal" ) -- the default Corona renderer, with no collision outli-nes

----------------------------------------------------------------------
-- Global val
----------------------------------------------------------------------
local gameScreen;
local mainMenuScreen;
local mainPig;
local enemyGroup;
local touchInfos = {}
local touchSplashs = {};

local gGameMode = "classic";
local gIsGameover = false;
local gScore = 0;

local gLastSpawnTime = 0;
local gSpawnTimer = 0;
for i=1,5 do
	local info = {};
	info.free = true;
	info.id = 0;
	info.beganTime = 0;
	info.endedTime = 0;
	info.comboKillCount = 0;
	info.lastKillTime = 0;
	touchInfos[i] = info;
end

-- Tweak gameplay
local gComboKillTimer = 0.5;

local gBlueBirdDivTime = 3500;
local gBlueBirdDivMaxY = 130;
local gBlueBirdDivMinY = 30;
local gBlueBirdDivSpeedFactor = 0.65;
local gBlueBirdDivDeltaSpeedX = 50;
local gBlueBirdDivDeltaSpeedY = 30;

local gGreenBirdDeltaXReturn = 150;

				
local fps = 60;
----------------------------------------------------------------------
-- End of Global val
----------------------------------------------------------------------

----------------------------------------------------------------------
-- Init Game
----------------------------------------------------------------------
function InitGame()
	mainMenuScreen = display.newGroup()	;
	gameScreen = display.newGroup()	;	
	
	-- Temp
	InitLevel();
end

function InitLevel()
	
	gIsGameover = false;
	gScore = 0;
	
	gameScreen.layer1 = display.newGroup();
	gameScreen.layer2 = display.newGroup();
	gameScreen.layer3 = display.newGroup();
	gameScreen.birds = display.newGroup();
	gameScreen.layer4 = display.newGroup();	
	gameScreen.layer5 = display.newGroup();	
	gameScreen.hud = display.newGroup()	;	
	
	gameScreen:insert(gameScreen.layer1)
	gameScreen:insert(gameScreen.layer2)
	gameScreen:insert(gameScreen.layer3)
	gameScreen:insert(gameScreen.birds)
	gameScreen:insert(gameScreen.layer4)
	gameScreen:insert(gameScreen.layer5)
	gameScreen:insert(gameScreen.hud)
	
	local scoreText = display.newText("0", 50, 10, native.systemFont, 20);
	scoreText:setTextColor(255, 0, 0);
	gameScreen.hud:insert(scoreText);
	gameScreen.hud.score = scoreText;
	
	if( gGameMode == "classic" ) then
		local heathText = display.newText("0", 420, 10, native.systemFont, 20);
		heathText:setTextColor(0, 255, 0);
		gameScreen.hud:insert(heathText);
		gameScreen.hud.heath = heathText;
	end
	
	enemyGroup = gameScreen.birds;
	
	-- bkg
	local bkg = display.newImage( "Resource/bkg.jpg", -50, -40 );		
	gameScreen.layer1:insert(bkg)
	
	-- Ground
	local ground = display.newImage( "Resource/ground.png", -50, 320-40 );	
	local groundFilter = { categoryBits = 4, maskBits = 3 } 
	physics.addBody( ground, "static", { friction = 0.3,filter=groundFilter } )
	gameScreen.layer3:insert(ground)
	
	-- Pig
	local pig = display.newImage( "Resource/lon.png", 200, 220 );	
	local pigFilter = { categoryBits = 1, maskBits = 6 } 
	pig.myDirection = 1;
	pig.heath = 3;
	pig.shieldAfterHitTime = 1000;
	physics.addBody( pig, { density = 1, friction = 0.3, bounce = 0.2, radius = 22 , filter=pigFilter } )
	gameScreen.layer4:insert(pig)
	
	mainPig = pig;
	pig.isFixedRotation = true
	
	-- Reset global val
	for i=1,5 do		
		touchInfos[i].free = true;		
		touchInfos[i].comboKillCount = 0;		
	end
end

function DeinitLevel()	
	-- Remove all display obj	
	for i = 1, gameScreen.numChildren do		
		gameScreen[1]:removeSelf( )
	end		
	-- Recreate
	timer.performWithDelay(100, InitLevel )
end

----------------------------------------------------------------------
-- GameOver Function
----------------------------------------------------------------------
function GameOver()
	gIsGameover=true;
	dieText = display.newText("DIE", 180,140, native.systemFont, 50)
	dieText:setTextColor(255, 0, 0)	
	gameScreen.hud:insert(dieText);
	timer.performWithDelay(3000, DeinitLevel )	
end

----------------------------------------------------------------------
-- Misc Function
----------------------------------------------------------------------
function GetTouchInfo(id)
	for i=1,5 do
		if touchInfos[i].id==id and touchInfos[i].free ==false then
			return touchInfos[i];					
		end
	end	
	return nil;
end

function AngleBetweenVector(v1,v2)
	local dotproduct = v1.x*v2.x + v1.y*v2.y;
	local length1 = math.sqrt( v1.x*v1.x + v1.y*v1.y );
	local length2 = math.sqrt( v2.x*v2.x + v2.y*v2.y );
	
	local cosa = dotproduct/ (length1*length2);
	local angle = math.acos(cosa)* 180 / math.pi;
	return angle;
end

----------------------------------------------------------------------
-- Pig
----------------------------------------------------------------------
function HandlePigCollideBird(self,event)
	if gGameMode == "classic" then
		if (mainPig.shieldAfterHitTime == 1000 ) then 
			mainPig.heath = mainPig.heath -1;
			mainPig.shieldAfterHitTime = 999;
		end
		
		if ( mainPig.heath==0 ) then
			GameOver();
		end
	else
	
	end
end
----------------------------------------------------------------------
-- Bird
----------------------------------------------------------------------

local function onBirdCollision( self, event )
	if( event.other == mainPig and gIsGameover==false and self.type~="black"  ) then
		HandlePigCollideBird(self,event);
	end	
end		

function newBird(params)
	local bird;	
	-------------------------------------------------------------------
	-- Red Bird 
	-------------------------------------------------------------------
	if( params.type == "red") then
		bird = display.newImage( "Resource/chim_do.png",params.x,params.y );		
		local birdFilter = { categoryBits = 2, maskBits = 5 } 		
	
		physics.addBody( bird, { density = 1, friction = 2000, bounce = 0.4, radius = 20, filter=birdFilter } )		
		-- public func						
		bird.size = 32;	
	end			
	
	-------------------------------------------------------------------
	-- Black Bird 
	-------------------------------------------------------------------
	if( params.type == "black") then
		bird = display.newImage( "Resource/chim_boom.png",params.x,params.y );		
		local birdFilter = { categoryBits = 2, maskBits = 4 } 		
		
		physics.addBody( bird, { density = 1, friction = 0.1, bounce = 0.2, radius = 20, filter=birdFilter } )		
		-- public func						
		bird.size = 24;	
	end		

	-------------------------------------------------------------------
	-- Yellow Bird 
	-------------------------------------------------------------------
	if( params.type == "yellow") then
		bird = display.newImage( "Resource/chim_vang.png",params.x,params.y );		
		local birdFilter = { categoryBits = 2, maskBits = 5 } 
				
		physics.addBody( bird, { density = 0.0, friction = 0.6, bounce = 0.2, radius = 16, filter=birdFilter } )		
		
		bird.bodyType = "kinematic";
		-- public func						
		bird.size = 32;	
	end
	
	-------------------------------------------------------------------
	-- Blue Bird 
	-------------------------------------------------------------------
	if( params.type == "blue") then
		bird = display.newImage( "Resource/chim_blue.png",params.x,params.y );		
		local birdFilter = { categoryBits = 2, maskBits = 5 } 
				
		physics.addBody( bird, { density = 1, friction = 1, bounce = 0.4, radius = 12, filter=birdFilter } )		
		
		-- public func						
		bird.size = 26;	
	end
	
	-------------------------------------------------------------------
	-- Green Bird 
	-------------------------------------------------------------------
	if( params.type == "green") then
		bird = display.newImage( "Resource/chim_green.png",params.x,params.y );		
		local birdFilter = { categoryBits = 2, maskBits = 5 } 
				
		physics.addBody( bird, { density = 1, friction = 1, bounce = 0.4, radius = 16, filter=birdFilter } )		
		
		-- public func						
		bird.size = 32;	
	end
	
	
	bird.timelife = 5000;
	
	bird.status = "normal";
	
	if( params.flip == 1 ) then
		bird.xScale = -1;
	end
	
	--bird:addEventListener("touch", bird );
	
	bird:setLinearVelocity( params.vx, params.vy );
	bird.angularVelocity = params.rotspd;
	bird.type = params.type;
	bird.rotation = params.rotation;
	
	bird.isChildBird = false;
	bird.isReturned = false;
	params.group:insert(bird);		
	
	function bird:update(deltaTime)				
		if self.timelife>0 then
			self.timelife = bird.timelife - deltaTime;
		else
			self.status = "die";
		end		
		
		if( self.type=="yellow") then
			if( self.bodyType == "kinematic" and self.y > 250 ) then
				self.bodyType = "dynamic";
			end
		end
		
		if( self.type=="blue" ) then
			if( self.timelife < gBlueBirdDivTime and self.isChildBird == false and self.y < gBlueBirdDivMaxY and self.y > gBlueBirdDivMinY ) then
				local parent_vx, parent_vy = self:getLinearVelocity()
				parent_vx = parent_vx*gBlueBirdDivSpeedFactor;
				parent_vy = parent_vy*gBlueBirdDivSpeedFactor;
				local dvx = gBlueBirdDivDeltaSpeedX;
				local dvy = gBlueBirdDivDeltaSpeedY;
				-- Spawn 3 blue birds
				self.status = "die";				
				local r_side;
				if( self.xScale == -1 ) then
					r_side = 1;
				else
					r_side = 0;
				end
				
				local b1 = newBird{ type="blue",
				  x= self.x,
				  y= self.y,
				  group = gameScreen.birds,
				  vx = parent_vx,
				  vy = parent_vy,
				  rotation = bird.rotation,
				  rotspd = bird.angularVelocity,
				  flip = r_side}
				b1.isChildBird = true;
				
				local b2_vx,b2_vy,b3_vx,b3_vy;
				if(r_side==0 ) then
					b2_vx = parent_vx+dvx;
					b2_vy = parent_vy-dvy;
					
					b3_vx = parent_vx-dvx;
					b3_vy = parent_vy;
				else
					b2_vx = parent_vx+dvx;
					b2_vy = parent_vy;
					
					b3_vx = parent_vx-dvx;
					b3_vy = parent_vy-dvy;
				end
				local b2 = newBird{ type="blue",
				  x= self.x,
				  y= self.y,
				  group = gameScreen.birds,
				  vx = b2_vx,
				  vy = b2_vy,
				  rotation = bird.rotation,
				  rotspd = bird.angularVelocity,
				  flip = r_side}
				b2.isChildBird = true;
				
				local b3 = newBird{ type="blue",
				  x= self.x,
				  y= self.y,
				  group = gameScreen.birds,
				  vx = b3_vx,
				  vy = b3_vy,
				  rotation = bird.rotation,
				  rotspd = bird.angularVelocity,
				  flip = r_side}
				b3.isChildBird = true;
				
				self.isAwake = false;
			end
		end
		
		if( self.type=="green" and self.isReturned == false and self.y < 220 ) then
			local delta_x = self.x - mainPig.x;
			local needReturn = false;		
			local returnAngularVel = 1500;			
			
			local vel_factor_x = 1;			
			local vel_factor_y = 0.6;			
			if( self.xScale == 1 ) then
				if( delta_x > gGreenBirdDeltaXReturn ) then
					self.angularVelocity = -returnAngularVel;
					
					local pig_vx, pig_vy = mainPig:getLinearVelocity()			
					local target_x = mainPig.x;
					local target_y = mainPig.y - 32;
					local return_vx = (target_x - self.x)*vel_factor_x;
					local return_vy = (target_y - self.y)*vel_factor_y;
					self:setLinearVelocity( return_vx, return_vy );
					
					self.isReturned = true;
				end
			else
				if( delta_x < -gGreenBirdDeltaXReturn ) then
					self.angularVelocity = returnAngularVel;
					
					local pig_vx, pig_vy = mainPig:getLinearVelocity()			
					local target_x = mainPig.x;
					local target_y = mainPig.y - 32;
					local return_vx = (target_x - self.x)*vel_factor_x;
					local return_vy = (target_y - self.y)*vel_factor_y;
					self:setLinearVelocity( return_vx, return_vy );
					
					self.isReturned = true;
				end
			end			
			
		end
		
	end		
	
	bird.collision = onBirdCollision
	
	bird:addEventListener( "collision", bird )
	
	function bird:onTouch(event) 
		--if(car.touchStatus
	end
	
	
	
	return bird;
end

----------------------------------------------------------------------
-- DieByBomb Function
----------------------------------------------------------------------

function DieByBomb( bird )  		
	bombText = display.newText("BOOM!", bird.x,bird.y, native.systemFont, 35)
	bombText:setTextColor(255, 127, 36)	
	gameScreen.hud:insert(bombText);	
	GameOver();
end

function HandleCombo( event, resetToOne)
	local touch = GetTouchInfo(event.id);
	
	if( touch==nil ) then
		return;
	end
	
	if(touch.comboKillCount>=3) then
		local comboString = string.format("Combo %d\n +%d", touch.comboKillCount,touch.comboKillCount);
		local comboText = display.newText(comboString, event.x-50,event.y-15, native.systemFont, 20)
		comboText:setTextColor(235, 63, 20)	;
		gameScreen.hud:insert(comboText);
		function comboText:timer( event )
			self:removeSelf();
		end 
		timer.performWithDelay(1000, comboText )
   
		gScore = gScore+touch.comboKillCount;

		
	end	
	
	if(resetToOne) then
		touch.comboKillCount	= 1; 
	else
		touch.comboKillCount	= 0;
	end
end
----------------------------------------------------------------------
-- handleTouch Function
----------------------------------------------------------------------

function HandleTouch( event )  	
	
	if( gIsGameover == true) then
		return;
	end
		
	if event.phase=="began" then		
		for i=1,5 do
			if touchInfos[i].free==true then				
				touchInfos[i].free = false
				touchInfos[i].id = event.id
				touchInfos[i].footprint = {};
				touchInfos[i].footprint[1] = {event.x,event.y, event.time};				
				break
			end
		end	
	end
		
	if event.phase=="moved" then
		local touch = GetTouchInfo(event.id);		
		if( touch==nil ) then
			return;
		end
		touch.footprint[ #(touch.footprint) +1] = {event.x,event.y, event.time};
			
		-- Check touch line for combo life		
		local footprint = touch.footprint;
		local point_count = #footprint;
		if point_count>= 3 then				
			local v1 = {};
			local v2 = {};
			v1.x =	footprint[point_count][1] - footprint[point_count-1][1];
			v1.y =	footprint[point_count][2] - footprint[point_count-1][2];
			
			v2.x =	footprint[point_count-1][1] - footprint[point_count-2][1];
			v2.y =	footprint[point_count-1][2] - footprint[point_count-2][2];
			
			local angle = AngleBetweenVector(v1,v2);			
			--print(angle);
			if(angle > 30 ) then				
				HandleCombo(event,false);
			end
		end
	
		-- kill birds
		local enemyNum = enemyGroup.numChildren
		for i = 1, enemyNum do		
			if( i<= enemyNum) then						
				local enemy = enemyGroup[i]	

				if( enemy.status == "normal" ) then			
					local dx = event.x- enemy.x;
					local dy = event.y- enemy.y;
					local dd = dx*dx + dy*dy;	
					if( dd < enemy.size*enemy.size ) then	
						if( enemy.type=="black" ) then
							enemy.status = "die";
							DieByBomb(enemy);
						else					
							enemy.status = "die";		
							local delta_time = (event.time-touch.lastKillTime)/1000;						
							if(delta_time<gComboKillTimer) then
								touch.comboKillCount = touch.comboKillCount+1;
							else
								HandleCombo(event,true);
							end						
							
							touch.lastKillTime = event.time;						
							gScore = gScore+1;								
						end
					end
				end
			end			
		end	
	
	end
	
	if event.phase == "ended" or event.phase == "cancelled" then
		HandleCombo(event,false);
		for i=1,5 do
			if touchInfos[i].id==event.id then
				touchInfos[i].free = true
				touchInfos[i].endedTime = event.time
			end
		end	
	end		
	
end 

function spawnBird()	
	local rand_number = math.random (1,4);
	
	
	for i=0,rand_number do	
		
		local bird_type_factor = math.random(1,100);
		local bird_type;
		if(bird_type_factor<=60) then
		   bird_type = "red";
		end	
		
		if(bird_type_factor>60 and bird_type_factor<=70) then
			bird_type = "green";			
		end
		
		if(bird_type_factor>70 and bird_type_factor<=80) then
			bird_type = "yellow";			
		end
		
		if(bird_type_factor>80 and bird_type_factor<=90) then
			bird_type = "blue";			
		end
		
		if(bird_type_factor>90 and bird_type_factor<=100) then
			bird_type = "black";			
		end			
		
		if( bird_type=="red") then		
			local r_side = math.random(0,1);
			local r_x = 0;
			if r_side==0 then
				r_x = math.random (-100, 0);		
			else
				r_x = math.random (480, 580);		
			end
			
			local r_y = -100;
			if( r_x < 0 or r_x > 480 ) then
				r_y = math.random( -100, 220 ) ;
			end
					
			local factor_vx = math.random( 30, 45 ) / 100;
			local factor_vy = math.random( -50, 0 ) / 100;
			local r_vx = (mainPig.x - r_x)*factor_vx;
			local r_vy = (mainPig.y - r_y)*factor_vy;
			
			local r_rot = math.random( 0, 180);
			
			local factor_rotspd = math.random( 90, 110 ) / 100;			
			local r_rotspd = (r_vx +r_vy)*factor_rotspd;
			
			local b = newBird{ type="red",
				  x= r_x,
				  y= r_y,
				  group = gameScreen.birds,
				  vx = r_vx,
				  vy = r_vy,
				  rotation = r_rot,
				  rotspd = r_rotspd,
				  flip = r_side}
		end
		
		if( bird_type=="black") then
			local r_side = math.random(0,1);
			local r_x = 0;
			if r_side==0 then
				r_x = math.random (-100, -50);		
			else
				r_x = math.random (530, 580);		
			end
			
			local r_y = math.random( 100, 220 ) ;
					
			local factor_vx = math.random( 80, 80 ) / 100;
			local factor_vy = math.random( 60, 60 ) / 100;
						
			local factor_height = math.random( 350, 350 );
			
			local r_vx = (mainPig.x - r_x)*factor_vx;
			local r_vy = (mainPig.y-factor_height - r_y)*factor_vy;
			
			local r_rot = math.random( 0, 180);
			
			local factor_rotspd = math.random( 60, 80 ) / 100;			
			local r_rotspd = ( math.abs(r_vx) + math.abs(r_vy) )*factor_rotspd;
			if r_side==0 then
				r_rotspd = r_rotspd;
			else
				r_rotspd = -r_rotspd;
			end
			
			local b = newBird{ type="black",
				  x= r_x,
				  y= r_y,
				  group = gameScreen.birds,
				  vx = r_vx,
				  vy = r_vy,
				  rotation = r_rot,
				  rotspd = r_rotspd,
				  flip = r_side}
		end
		
		if( bird_type=="yellow") then		
			local r_side;
			if(mainPig.x > 240 ) then
				r_side = 0;
			else
				r_side = 1;
			end
				
			local r_x = 0;
			if r_side==0 then
				r_x = math.random (-100, 50);		
			else
				r_x = math.random (430, 580);		
			end
			
			local r_y = -100;
			if( r_x < 0 or r_x > 480 ) then
				r_y = math.random( -100, 220 ) ;
			end
			
			local pig_vx, pig_vy = mainPig:getLinearVelocity()
			--print(pig_vx);
			local target_x = mainPig.x + pig_vx*1.5;
			local target_y = mainPig.y-32;
			print(target_x," ",target_y);
			local factor_v = 250;
			local r_vx = (target_x - r_x);
			local r_vy = (target_y - r_y);
			local len = math.sqrt( r_vx*r_vx + r_vy*r_vy );
			local r_vx = r_vx*factor_v/len;
			local r_vy = r_vy*factor_v/len;
			local v1 = {};
			local v2 = {};
			v1.x = r_vx;
			v1.y = r_vy;
			local r_rot;
			if( r_side == 0 ) then
				v2.x = 1;
				v2.y = 0;
				r_rot = AngleBetweenVector(v1,v2);		
			else
				v2.x = -1;
				v2.y = 0;
				r_rot = -AngleBetweenVector(v1,v2);		
			end			
			
			local r_rotspd = 0;
			
			local b = newBird{ type="yellow",
				  x= r_x,
				  y= r_y,
				  group = gameScreen.birds,
				  vx = r_vx,
				  vy = r_vy,
				  rotation = r_rot,
				  rotspd = r_rotspd,
				  flip = r_side}
		end
		
		if( bird_type=="blue") then		
			local r_side = math.random(0,1);
			local r_x = 0;
			if r_side==0 then
				r_x = math.random (-100, -50);		
			else
				r_x = math.random (530, 580);		
			end
			
			r_y = math.random( -50, 150 ) ;			
					
			local factor_vx = math.random( 30, 40 ) / 100;
			local factor_vy = math.random( -50, -30 ) / 100;
			local r_vx = (mainPig.x - r_x)*factor_vx;
			local r_vy = (mainPig.y - r_y)*factor_vy;
			
			local r_rot = math.random( 0, 180);
			
			local factor_rotspd = math.random( 90, 110 ) / 100;			
			local r_rotspd = (r_vx +r_vy)*factor_rotspd;
			
			local b = newBird{ type="blue",
				  x= r_x,
				  y= r_y,
				  group = gameScreen.birds,
				  vx = r_vx,
				  vy = r_vy,
				  rotation = r_rot,
				  rotspd = r_rotspd,
				  flip = r_side}
		end
		
		if( bird_type=="green") then
			local r_side = math.random(0,1);
			local r_x = 0;
			if r_side==0 then
				r_x = math.random (-100, -50);		
			else
				r_x = math.random (530, 580);		
			end
			
			local r_y = math.random( 100, 220 ) ;
					
			local factor_vx = math.random( 80, 80 ) / 100;
			local factor_vy = math.random( 60, 60 ) / 100;
						
			local factor_height = math.random( 350, 500 );
			
			local r_vx = (mainPig.x - r_x)*factor_vx;
			local r_vy = (mainPig.y-factor_height - r_y)*factor_vy;
			
			local r_rot = math.random( 0, 180);
			
			local factor_rotspd = math.random( 30, 40 ) / 100;			
			local r_rotspd = ( math.abs(r_vx) + math.abs(r_vy) )*factor_rotspd;
			if r_side==0 then
				r_rotspd = r_rotspd;
			else
				r_rotspd = -r_rotspd;
			end
			
			local b = newBird{ type="green",
				  x= r_x,
				  y= r_y,
				  group = gameScreen.birds,
				  vx = r_vx,
				  vy = r_vy,
				  rotation = r_rot,
				  rotspd = r_rotspd,
				  flip = r_side}
		end
		
	end		
end

----------------------------------------------------------------------
-- GameLoop Function
----------------------------------------------------------------------
local previousFrameTime = 0;
function GameLoop( event )   		

	-- HUD
	gameScreen.hud.score.text = gScore;
	gameScreen.hud.heath.text = mainPig.heath;
	
	if gIsGameover==true then
		return
	end
	
	local currTime = event.time;
	local frameDiffTime = currTime-previousFrameTime;
	-- Update pig
	local pig_move_distance = 100;
	local curr_pig_vx,curr_pig_vy = mainPig:getLinearVelocity();
	if( mainPig.myDirection==0) then
		mainPig:setLinearVelocity(30,curr_pig_vy);
		if( mainPig.x > 240 + pig_move_distance ) then
			mainPig.myDirection = 1;
		end
	else
		mainPig:setLinearVelocity(-30,curr_pig_vy);
		if( mainPig.x < 240 - pig_move_distance ) then
			mainPig.myDirection = 0;
		end
	end
	
	if( mainPig.shieldAfterHitTime < 1000 ) then
		mainPig.shieldAfterHitTime = mainPig.shieldAfterHitTime-frameDiffTime;
	end
	
	if( mainPig.shieldAfterHitTime < 0 ) then
		mainPig.shieldAfterHitTime = 1000;
	end	
	
	local touchLiveTimer = 100.0;
	--Kill dead touch
	for i=1,5 do
		if touchInfos[i].free==false then
			-- draw
			local footprint = touchInfos[i].footprint;
			local len = #footprint;
			for j=1,len do	
				if( j<= len) then
					--print(currTime, footprint[j][3])
					if ( currTime-footprint[j][3] > touchLiveTimer ) then
						table.remove (footprint,j);	
						len = len-1;
						j = j-1;
						--print("kill")					
					end
				end
			end			
		end
	end	
	
	--Draw touch
	for i=1,5 do
		if touchSplashs[i] then
			touchSplashs[i]:removeSelf();
			touchSplashs[i] = nil;
		end
		
		if touchInfos[i].free==false then
			-- draw
			local footprint = touchInfos[i].footprint;
			if #footprint> 2 then				
				touchSplashs[i] = display.newLine(footprint[1][1],footprint[1][2],footprint[2][1],footprint[2][2]);
				
				touchSplashs[i]:setColor( 255, 102, 102, 255 )
				touchSplashs[i].width = 3 
				for j=3,#footprint do
					touchSplashs[i]:append(footprint[j][1],footprint[j][2]);
				end			
			end						
		end
	end		
	
	-- touchInfos[i].drawer = display.newLine(event.x,event.y,event.x+10,event.y);
				-- gameScreen.layer5:insert(touchInfos[i].drawer);
				-- touchInfos[i].drawer:setColor( 255, 0, 0, 255 );				
				-- touchInfos[i].drawer.width = 3; 
	
	-- Kill died bird
	local enemyNum = enemyGroup.numChildren
	for i = 1, enemyNum do		
		if( i<= enemyNum) then
			local enemy = enemyGroup[i]		
			if( enemy.status=="die" ) then						
				enemy:removeSelf()
				i = i-1
				enemyNum = enemyNum-1
			end
		end
	end
	
	-- update bird
	local enemyNum = enemyGroup.numChildren
	for i = 1, enemyNum do		
		if( i<= enemyNum) then
			local enemy = enemyGroup[i]		
			enemy:update(frameDiffTime);
		end
	end
	
	-- Spawn Bird
	if gSpawnTimer == 0 then
		gSpawnTimer = math.random( 1500,3000);
	end
	
	if currTime- gLastSpawnTime > gSpawnTimer then
		spawnBird();
		gSpawnTimer = 0;
		gLastSpawnTime = currTime;
	end
	
	previousFrameTime = currTime;
end 

Runtime:addEventListener( "memoryWarning", HandleLowMemory );
Runtime:addEventListener( "enterFrame", GameLoop );
Runtime:addEventListener( "touch", HandleTouch );
InitGame();